import webapp2
import jinja2
import os
from google.appengine.api import users
from google.appengine.ext import ndb
import city
import dic
import labelcat


jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

    
class Country(ndb.Model):
    name = ndb.StringProperty()
    style = ndb.StringProperty()
    culture = ndb.StringProperty()
    user = ndb.StringProperty()
    era = ndb.StringProperty()

class CountrySys(ndb.Model):
    name = ndb.TextProperty()
    style = ndb.TextProperty()
    culture = ndb.TextProperty()
    era = ndb.StringProperty()  

  
class Info(webapp2.RequestHandler):
    def get(self, lang, archive_name, era_name, country_name):
        
        template_values = {
            'lang':lang,
            'archive':archive_name,
            'era':era_name,
            'country':country_name,
        }
        template = jinja_environment.get_template('geo.html')
        self.response.out.write(template.render(template_values))



class Index(webapp2.RequestHandler):
    def get(self, lang, archive_name, era_name, country_name):
        instance = dic.Operation()
        country_operation_map = {}
        dics = instance.getDics(lang, labelcat.Cat.COUNTRY_MANAGEMENT)
        for dic_entity in dics:
            country_operation_map[dic_entity.name] = dic_entity.label
        template_values = {
            'lang':lang,
            'archive':archive_name,
            'era':era_name,
            'country':country_name,
            'countryLabels':country_operation_map
        }
        template = jinja_environment.get_template('country.html')
        self.response.out.write(template.render(template_values))

class CityList(webapp2.RequestHandler):
    def get(self, lang, archive_name, era_name, country_name):
        instance = dic.Operation()
        city_map = {}
        dics = instance.getDics(lang, labelcat.Cat.City)
        for dic_entity in dics:
            city_map[dic_entity.name] = dic_entity.label   
        dics = instance.getDics(lang, labelcat.Cat.Geo)
        geo_map = {}
        for dic_entity in dics:
            geo_map[dic_entity.name] = dic_entity.label
            
        city_key = "_" + archive_name       
        cities = city.City.query(city.City.archive == city_key)
        
        template_values = {
            'lang':lang,
            'archive':archive_name,
            'era':era_name,
            'country':country_name,
            'cities':cities,
            'cityLabels':city_map,
            'geoLabels':geo_map
        }
        template = jinja_environment.get_template('city-list.html')
        self.response.out.write(template.render(template_values))


        
        
class AddPage(webapp2.RequestHandler):
    def get(self):
        if users.get_current_user():
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'Logout'
        else:
            url = users.create_login_url(self.request.uri)
            url_linktext = 'Login' 
             
        template_values = {
            'url': url,
            'url_linktext': url_linktext,
        }
        template = jinja_environment.get_template('country-create.html')
        self.response.out.write(template.render(template_values))


class Add(webapp2.RequestHandler):
    def post(self):
        owner = "AI"
        if users.get_current_user():
            owner = users.get_current_user().email()    
                      
        name = self.request.get("country-name")
        style = self.request.get("country-style")
        culture = self.request.get("country-cultrue")
        countries = Country.query(Country.owner == owner, Country.name == name).fetch(1)
        if len(countries) > 0:
            country = countries[0]
            country.style = style
            country.culture = culture
        else:
            country = Country(name=name, style=style, culture=culture, user=owner)
        country.put()
        self.redirect("/game")
